﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassFolio.Model;
using System.ComponentModel;
using System.Windows.Input;
using ClassFolio;
using System.Windows;

namespace GeneFolio.ViewModel
{
    class LoginViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Fields

        RelayCommand _connectCommand;
        RelayCommand _exitCommand;

        string _login;
        string _server;
        string _password;

        internal event Delegats.DialogResult OnDialgoResult;
        internal event Delegats.ChangeCurType OnChangeCurType;

        #endregion

        #region Constructors
        public LoginViewModel()
        { 
            _login = "";
            _server = "";
            _password = "";
        }
                
        #endregion

        #region Presentation properties

        public ICommand ConnectCommand
        {
            get
            {
                if (_connectCommand == null)
                {
                    _connectCommand = new RelayCommand(
                        param => this.Connect(),
                        param => this.CanConnect
                        );
                }
                return _connectCommand;
            }
        }
        public ICommand ExitCommand
        {
            get
            {
                if (_exitCommand == null)
                {
                    _exitCommand = new RelayCommand(
                        param => this.Exit()
                        );
                }
                return _exitCommand;
            }
        }
        
        #endregion

        #region LoginView properties

        public string Server 
        {
            get { return this._server;}
            set
            {
                _server = value;
                base.OnPropertyChanged("Server");
            } 
        }
        public string Login 
        {
            get { return this._login; }
            set 
            {
                _login = value;
                base.OnPropertyChanged("Login");
            } 
        
        }
        public string Password
        {
            get { return this._password; }
            set 
            {
                _password = value;
                base.OnPropertyChanged("Password");
            } 
        }

        /// <summary>
        /// Список пользователей системы
        /// </summary>
        public string[] LoginUsers
        {
            get
            {
                return ClassFolio.Tools.GetUserList().Items.Select(t => t.Name).ToArray();
            }
        }
        /// <summary>
        /// Список серверов для подключения
        /// </summary>
        public string[] Servers
        {
            get
            {
                return ClassFolio.Tools.GetServerList().Items.Select(t => t.Name).ToArray();

            }
        }
        #endregion

        #region Public methods
        
        public void Connect()
        {
            if (OnChangeCurType != null) OnChangeCurType(Cursors.Wait);
            try
            {
                AreaWork.LoginAs(Login, Password, Server);
                Tools.SaveUserList(Tools.GetUserList().Add(new ComboBoxItem(Login)));
                Tools.SaveServerList(Tools.GetServerList().Add(new ComboBoxItem(Server)));
                if (OnDialgoResult != null) OnDialgoResult(true);
            }
            finally
            {
                if (OnChangeCurType != null) OnChangeCurType(Cursors.Arrow);
            }

        }

        public void Exit()
        {
            if (OnChangeCurType != null) OnChangeCurType(Cursors.Wait);
            try
            {
                if (OnDialgoResult != null) OnDialgoResult(false);
            }
            finally
            {
                if (OnChangeCurType != null) OnChangeCurType(Cursors.Arrow);
            }
        }
        
        #endregion

        #region Private helpers

        private bool CanConnect
        {
            get { return !String.IsNullOrEmpty(this.Login) && !String.IsNullOrEmpty(this.Server) && !String.IsNullOrEmpty(this.Password); }
        }

        #endregion

        #region IDataErrorInfo members
        
        string IDataErrorInfo.Error
        {
            get { return null; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                switch (propertyName)
                {
                    case "Login":
                        if ( Tools.IsStringMissing(this.Login) || this.Login.Length > 32 )
                            return ValidErrors.User_Error_MissingUserLogin;
                        break;
                    case "Password":
                        if (Tools.IsStringMissing(this.Password) || this.Password.Length > 32)
                            return ValidErrors.User_Error_MissingPassword;
                        break;
                    case "Server":
                        if (Tools.IsStringMissing(this.Server) || this.Server.Length > 32 )
                            return ValidErrors. User_Error_MissingUserLogin;
                        break;
                    default:
                        error = "Error";
                        break;
                }

                CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }

        #endregion
    }
}
